<template>
  <div>
    <el-dialog
      :title="title"
      :visible="visible"
      @update:visible="$emit('update:visible', $event)"
      @closed="onClosed"
    >
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item
          label="部门名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="部门编码"
          :label-width="formLabelWidth"
          prop="code"
        >
          <el-input v-model="form.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="部门负责人"
          :label-width="formLabelWidth"
          prop="manager"
        >
          <el-select
            v-model="form.manager"
            placeholder="请选择"
            @focus="addEmployees()"
          >
            <el-option
              :label="item.username"
              :value="item.username"
              v-for="item in manager"
              :key="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="部门介绍"
          :label-width="formLabelWidth"
          prop="introduce"
        >
          <el-input v-model="form.introduce" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('resetData')">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDepartments, editDepartments } from '@/api/department'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: 'depDialog',
  props: {
    visible: Boolean,
    form: Object
  },
  computed: {
    title () {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  data () {
    return {
      manager: [],
      formLabelWidth: '90px',

      rules: {
        name: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'change' }
        ],
        code: [
          {
            min: 1,
            max: 50,
            message: '请输入1-50个字符',
            trigger: 'change'
          },
          { required: true, message: '输入不能为空', trigger: 'blur' }
        ],
        manager: [{ required: true, message: '输入不能为空', trigger: 'blur' }],
        introduce: [
          {
            min: 1,
            max: 300,
            message: '请输入1-300个字符',
            trigger: 'change'
          },
          { required: true, message: '输入不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onClosed () {
      this.$emit('resetData')
    },
    async addEmployees () {
      const res = await getEmployeeSimple()
      // console.log(res)
      this.manager = res
    },
    submitForm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            const res1 = await editDepartments(this.form)
            console.log('res1', res1)
          } else {
            // console.log(this.form)
            const res = await addDepartments(this.form)
            // console.log(res)
          }
          this.$message.success('新增成功')
          // this.getData()
          // this.resetData()
          this.$emit('getData')
          this.$emit('resetData')
        } else {
          this.$message.error('输入格式有误请检查')
          return false
        }
      })
    }
  }
}
</script>

<style></style>
